package net.pm.core.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.pm.common.dao.GenericDao;
import net.pm.common.service.impl.GenericServicerImpl;
import net.pm.common.toolkit.DevConstant;
import net.pm.common.toolkit.PMTool;
import net.pm.common.toolkit.Pager;
import net.pm.common.toolkit.date.DateUtils;
import net.pm.core.entities.db.Orderinfo;
import net.pm.core.entities.db.Orderinfolog;
import net.pm.core.entities.db.Pmuser;
import net.pm.core.entities.db.Product;
import net.pm.core.service.OrderServicer;
import net.pm.core.service.OrderinfologServicer;
import net.pm.core.service.ProductServicer;

import org.springframework.stereotype.Service;

@Service("ProductServicer")
public class ProductServicerImpl extends GenericServicerImpl<Product, Long> implements ProductServicer {

	@Resource(name="ProductDao")
	@Override
	public void setGenericDao(GenericDao<Product, Long> genericDao) {
		super.genericDao = genericDao;
	}

	@Resource(name="OrderServicer")
	private OrderServicer orderServicer;
	@Resource(name="OrderinfologServicer")
	private OrderinfologServicer orderinfologServicer;
	
	@Override
	public Pager<Product> findProductPager( String productName,
			Date startdate,Date enddate, Integer pagesize, Integer pageno) {
		final StringBuilder builder = new StringBuilder(" from Product product where  product.status = ? ");
		final List<Object> args = new ArrayList<Object>();
		args.add(Product.STATUS_FINISH);
		
		
		if(PMTool.isNotEmpty(productName)){
			builder.append(" and product.productName like ? ");
			args.add(productName);
		}
		if(PMTool.isNotEmpty(startdate)){
			builder.append(" and product.addTime >= ? ");
			args.add(startdate);
		}
		if(PMTool.isNotEmpty(enddate)){
			builder.append(" and product.addTime <= ? ");
			args.add(enddate);
		}
		builder.append(" order by product.addTime  desc ");
		
		return super.findPagerByJpql(pagesize, pageno, builder, args.toArray());
	}
	

	@Override
	public void finishProduce(Long orderId, Integer productNumber,
			String remark, Long userId) {
		Orderinfo orderinfo = orderServicer.findEntityById(orderId);
		//更新订单信息
		orderinfo.setStatus(Orderinfo.STATUS_FINISH);
		orderServicer.modify(orderinfo);
		
		//更新产品信息
		Product product = orderinfo.getProduct();
		product.setProductNumber(productNumber);
		super.modify(product);
		
		//记录订单日志
		Orderinfolog orderinfolog = new Orderinfolog();
		orderinfolog.setOperationTime(DateUtils.obtainCurrentDate());
		orderinfolog.setOrderinfo(new Orderinfo(orderId));
		orderinfolog.setRemark(remark);
		orderinfolog.setStatus(Orderinfo.STATUS_FINISH);
		orderinfolog.setUser(new Pmuser(userId));
		
		orderinfologServicer.create(orderinfolog);
		
	}

	@Override
	public void checkInLibrary(Long productId, Long userId) {
		Product product  = super.findEntityById(productId);
		//入库　setter 一些属性
		//入库时间
		product.setAddTime(DateUtils.obtainCurrentDate());
		//库品编号
		product.setProductNo(PMTool.generateUniqueno(DevConstant.PRODUCT_PREFIX));
		//入库操作者
		product.setPmuser(new Pmuser(userId));				
		//
		product.setStatus(Product.STATUS_FINISH);
		super.modify(product);
		
	}


}
